package com.taoyibao.hongcanting.ui.restaurant.fragment;

import android.os.Bundle;

import com.taoyibao.hongcanting.base.fragment.BaseFragmentPresenter;
import com.taoyibao.hongcanting.constant.CodeConstant;
import com.taoyibao.hongcanting.event.EventGift;
import com.taoyibao.hongcanting.model.GiftInfoModel;
import com.taoyibao.hongcanting.ui.restaurant.delegate.GiftPageFragmnetDelegate;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author TangAnna
 * @description: 礼物中的每一个page
 * @date :${DATA} 16:14
 */
public class GiftPageFragmnet extends BaseFragmentPresenter<GiftPageFragmnetDelegate> {
    private List<GiftInfoModel> mData;

    @Override
    protected Class<GiftPageFragmnetDelegate> getDelegateClass() {
        return GiftPageFragmnetDelegate.class;
    }

    public static GiftPageFragmnet getInstance(ArrayList<GiftInfoModel> giftData) {
        GiftPageFragmnet fragment = new GiftPageFragmnet();
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList(CodeConstant.GIFT, giftData);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void bindEvenListener() {
        super.bindEvenListener();
        EventBus.getDefault().register(this);
        mData = new ArrayList<>();
        ArrayList<GiftInfoModel> data = getArguments().getParcelableArrayList(CodeConstant.GIFT);
        if (data != null && data.size() > 0) {
            mData.addAll(data);
        }
        viewDelegate.setData(mData);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    /**
     * 送礼物的页面点击时收到的消息 发送时使用数据
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void EveentMessage(EventGift eventGift) {
        GiftInfoModel giftInfoModel = eventGift.mGiftInfoModel;
        if (giftInfoModel != null) {
            for (int i = 0; i < mData.size(); i++) {
                if (mData.get(i).equals(giftInfoModel)) {
                    mData.get(i).isCheckd = true;
                } else {
                    mData.get(i).isCheckd = false;
                }
            }
        } else {//页面切换时出现的情概况 （页面切换时取消所有选中的礼物）
            for (int i = 0; i < mData.size(); i++) {
                mData.get(i).isCheckd = false;
            }
        }

        viewDelegate.setData(mData);
    }
}
